package eng.jTrussSolver.Node;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class NodeHashMap extends HashMap<Integer, NodeData> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int _nodeCounter;
	/**
	 * Default Constructor 
	 */
	public NodeHashMap()
	{
		_nodeCounter=0;
	}



	/**
	 * Add Node
	 * @param nodeData
	 */
	public void addNode(NodeData nodeData) {
		this.put(_nodeCounter,nodeData);
		_nodeCounter = _nodeCounter + 1 ;
	}

	/**
	 * 
	 * @param currentselectednode
	 * @param b
	 */
	public void setNode(int currentselectednode, NodeData b) {
		this.remove(b.getNodeID());
		this.put(b.getNodeID(), b);

	}

	/**
	 * http://stackoverflow.com/questions/2338790/get-int-from-string-also-containing-letters-in-java
	 * Teleytaio comment (answered Feb 26 '10 at 0:57).
	 * @param jListElement
	 * @return The first Integer(ID) of JList Element(String) Value.
	 */
	public int getStringElementID(String jListElement) {
		int ID = 0;
		try {
			ID = ((Number)NumberFormat.getInstance().parse(jListElement)).intValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ID;
	}

	/**
	 * returns count of nodes.
	 * @return
	 */
	public int getNodesCount() {

		return this.size();
	}

	public int getNodeHashKey() {
		return this._nodeCounter;
	}


	public void removeNode(int aa) {
		this.remove(aa);
	}

	/**
	 * Code For Nodes Data
	 * @param doc
	 * @return
	 */
	public Element prepareXMLElement(Document doc, HashMap<Integer, NodeData> nodes) {

		Element N = doc.createElement("Nodes");

		for (Integer i : nodes.keySet()) {

			Element nodeElement = doc.createElement("Node");
			N.appendChild(nodeElement);

			Element g = doc.createElement("NodeID");
			g.appendChild(doc.createTextNode(String.valueOf((nodes.get(i).NodeID))));
			nodeElement.appendChild(g);

			Element c = doc.createElement("Coords");

			Element cx = doc.createElement("X");
			cx.appendChild(doc.createTextNode(String.valueOf(nodes.get(i).X)));
			nodeElement.appendChild(c).appendChild(cx);

			Element cy = doc.createElement("Y");
			cy.appendChild(doc.createTextNode(String.valueOf(nodes.get(i).Y)));
			nodeElement.appendChild(c).appendChild(cy);

			Element cz = doc.createElement("Z");
			cz.appendChild(doc.createTextNode(String.valueOf(nodes.get(i).Z)));
			nodeElement.appendChild(c).appendChild(cz);

			Element d =doc.createElement("Restrict_Moves");

			Element dx = doc.createElement("notx");
			dx.appendChild(doc.createTextNode(String.valueOf(nodes.get(i).notMoveX)));
			nodeElement.appendChild(d).appendChild(dx);

			Element dy = doc.createElement("noty");
			dy.appendChild(doc.createTextNode(String.valueOf(nodes.get(i).notMoveY)));
			nodeElement.appendChild(d).appendChild(dy);

		}
		return N;
	}
	/**
	 * Clears() Hashmap and also resets counter.
	 */
	public void clear()
	{
		super.clear();
		this._nodeCounter=0;
	}
}
